package com.goblin.model.uaa.controller;

import com.goblin.common.response.PageHelper;
import com.goblin.common.response.Paged;
import com.goblin.common.response.Response;
import com.goblin.common.util.MessageDigestUtil;
import com.goblin.model.base.BaseController;
import com.goblin.model.uaa.po.User;
import com.goblin.model.uaa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/uaa/user")
public class UserController extends BaseController<User> {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Response<Paged<User>> paged(PageHelper pageable) {
        Paged<User> users = this.userService.paged(pageable);
        return Response.success(users);
    }

    @PostMapping("/login")
    public Response login(@RequestBody User user) {
        Response response = this.userService.login(user);
        return response;
    }

    @Override
    public void before(User user) {
        super.before(user);
        String password = MessageDigestUtil.base64AndMD5(user.getPassword());
        user.setPassword(password);
    }

    @Override
    public <T> Object after(T t) {
        User user = (User) t;
        ((User) t).setPassword(null);
        return user;
    }
}
